////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////@
//
// Το παρόν service είναι εκπαιδευτικό και δείχνει έναν τρόπο χειρισμού
// των αιτημάτων προς τον Node server σε συνδυασμό με την προσπέλαση της
// database.

Log.print('service module: peparam');

Service.peparam = {};

Service.peparam.action = function(nodereq) {
	var conn = DB.connection();
	var query = 'SELECT `login`, `onoma` FROM `pektis`';
	if (nodereq.url.login) query += ' WHERE `login` LIKE ' + conn.escape(nodereq.url.login);
	query += ' ORDER BY `login`';
	conn.query(query, function(conn, rows) {
		Service.peparam.peparam(nodereq, conn, rows, 0);
	});
}

Service.peparam.peparam = function(nodereq, conn, pektis, i) {
	if (i >= pektis.length) {
		conn.free();
		nodereq.end();
		return;
	}

	var query = 'SELECT * FROM `peparam` WHERE `pektis` = BINARY ' + conn.connection.escape(pektis[i].login);
	conn.query(query, function(conn, rows) {
		nodereq.write(pektis[i].login + ' [' + pektis[i].onoma + ']\r\n');
		for (var j = 0; j < rows.length; j++) {
			nodereq.write('\t' + rows[j].param + ': ' + rows[j].timi + '\r\n');
		}

		Service.peparam.peparam(nodereq, conn, pektis, i + 1);
	});
}
